package academic.emailClient.view;

import java.io.StringWriter;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

/**
 * this class gets  and initializes a Velocity engine and read/write into a vm file *
 */
public class VelocityTemplate
{
	VelocityEngine ve;
	VelocityContext context;
	String RELATIVE_PATH = "Templates/";
	
	public VelocityTemplate() throws Exception{
        //Get and initialize an engine
        ve = new VelocityEngine();
        ve.init();
        context = new VelocityContext();
	}
	
	public String getTemplateString(String fineName) throws ResourceNotFoundException, ParseErrorException, Exception{
		//get the Template
		Template t = ve.getTemplate( "Templates/"+fineName);
		//render the template into a Writer
		StringWriter writer = new StringWriter();
        t.merge( context, writer );
	
        return writer.toString();
	} 

}
